<?php
/**
 * Created by b17
 * 3/22/12 8:21 PM
 */

use Managers\Builder\AdBuilder,
    Managers\Builder\UserPaymentBuilder,
    Managers\Category,
    Managers\Builder\NewsBuilder,
    Managers\Marks;


class Admin_StatisticController extends G4T_AdminController
{
    const ITEMS_PER_PAGE = 50;

    public function indexAction()
    {

    }

    public function paymentAction()
    {
        $userPaymentBuilder = new UserPaymentBuilder();

        $this->_redirectWhenPost();
        $this->_applyFilters($userPaymentBuilder);

        $payments = $userPaymentBuilder->getArrayResult();
        $this->view->payments = $payments;

        $total = $userPaymentBuilder->getTotal();
        $this->view->total = $total;
    }

    public function currentAction()
    {
        $adBuilder = new AdBuilder();
        $adBuilder->setOnlyPromoted();

        $total = $adBuilder->getTotal();
        $this->view->total = $total;

        $ads = $adBuilder->getList();
        $this->view->ads = $ads;
    }

    public function adAction()
    {
        $adBuilder = new AdBuilder();

        $this->_redirectWhenPost();
        $this->_applyFilters($adBuilder);

        $categoryManager = new Category();
        $categories = $categoryManager->getList();
        $this->view->categories = $categories;

        $total = $adBuilder->getTotal();
        $this->view->total = $total;

        $page = intval($this->_getParam('page'));
        $offset = $this->_getOffset($page, self::ITEMS_PER_PAGE);
        $this->displayPaginantor($total, $page, self::ITEMS_PER_PAGE);
        $adBuilder->setLimit(self::ITEMS_PER_PAGE, $offset);
        $ads = $adBuilder->getList();
        $this->view->ads = $ads;
    }

    public function newsAction()
    {
        $newsBuilder = new NewsBuilder();

        $this->_redirectWhenPost();
        $this->_applyFilters($newsBuilder);
        $newsBuilder->addOrderByViews();
        $total = $newsBuilder->getTotal();
        $this->view->total = $total;

        $page = intval($this->_getParam('page'));
        $this->displayPaginantor($total, $page, self::ITEMS_PER_PAGE);

        $offset = $this->_getOffset($page, self::ITEMS_PER_PAGE);
        $newsBuilder->setLimit(self::ITEMS_PER_PAGE, $offset);
        $news = $newsBuilder->getList();
        $marksManager = new Marks();
        $news = $marksManager->injectMarks($news);
        $this->view->news = $news;
    }

    /**
     * После получение фильтров, выполняем редирект на страницу просмотра.
     */
    private function _redirectWhenPost()
    {
        if ($this->_request->isPost()) {
            $params = $this->_getAllParams();
            $params = array_merge($params, $_POST);
            $this->_redirect($this->view->url($params));
        }
    }

    private function _applyFilters($builder)
    {
        foreach ($this->_getAllParams() as $key => $param) {
            $methodName = 'set' . ucfirst($key);
            if (method_exists($builder, $methodName)) {
                call_user_func(array($builder, $methodName), $param);
                $this->view->$key = $param;
            }
        }
    }
}
